//
// Created by wujehy on 2020/8/5.
//

#ifndef AUTH_RAFT_IOTMOD_H
#define AUTH_RAFT_IOTMOD_H

#include "BusinessBase.h"
#include "handy/event_base.h"

class NetWorkWarpper;

class BusinessManager;

class IoTDB;

class IoTMod : public BusinessBase<NetWorkWarpper>
{
    enum IOT_MOD_TYPE
    {
        IOT_UPLOAD_SENSOR_DATA = 1,
        IOT_FIND_SENSOR_DATA = 2,
        IOT_CONTROL_DEVICE = 3,
        IOT_CONTROL_ACK = 4,
        IOT_CONTROL_ACK_LISTERNER = 5,
        IOT_CONTROL_LISTERNER = 6,
        IOT_LOGIN_DEVICE = 7,
        IOT_HEART_BEAT = 8,
        IOT_FIND_GATEWAY = 9,
        IOT_CREATE_DEVICE = 10,
        IOT_FIND_DEVICE_TOKEN = 11,
        IOT_GENERATE_DEVICE_TOKEN = 12,
        IOT_FIND_MY_DEVICE = 13,
    };
    BusinessManager *businessManager = nullptr;
    std::map<int, BusinessBaseEventFunction> m_type_event_map;
    // NOTE wujehy 在这里实现回调是为了方便拿到该模块的类成员 信息
    BusinessBaseEventFunction uploadSensor_event_function;
    BusinessBaseEventFunction findSensor_event_function;
    BusinessBaseEventFunction controlDevice_event_function;
    BusinessBaseEventFunction controlDeviceAck_event_function;
    BusinessBaseEventFunction loginDevice_event_function;
    BusinessBaseEventFunction deviceHeartBeat_event_function;
    BusinessBaseEventFunction createDevice_event_function;
    BusinessBaseEventFunction findDeviceToken_event_function;
    BusinessBaseEventFunction generateDeviceToken_event_function;
    BusinessBaseEventFunction findMyDevices_event_function;
    BusinessBaseEventFunction findGateway_event_function;

    /**
     * @brief 初始化回调方法 , 即子业务 处理方法
     */
    void init_callback_function();

    /**
     * @brief 初始化 事件分发器
     */
    void init_event();

    IoTDB *ioTTable = nullptr;

    // 设备管理map
    std::map<std::string, handy::TcpConnPtr> m_token_client_map; ///< token 对应 client 的map
    std::map<std::string, handy::TcpConnPtr> m_device_code_client_map; ///< task 对应 client 的map
public:
    IoTMod();

    int type() override;

    int depackage(int taskid, int type, void *data, int len, void *userdata = nullptr) override;

    void setBusinessManager(BusinessManager *hander);

    void init_finished() override;
};


#endif //AUTH_RAFT_IOTMOD_H
